package com.zeyun.EasyOnlineShareCloud.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.util.SaResult;
import com.zeyun.EasyOnlineShareCloud.dto.req.admin.FileUpdateStatusReqDTO;
import com.zeyun.EasyOnlineShareCloud.dto.req.admin.UserAddReqDTO;
import com.zeyun.EasyOnlineShareCloud.dto.req.admin.UserEditReqDTO;
import com.zeyun.EasyOnlineShareCloud.service.AdminService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员控制器
 */
@RestController
@RequestMapping("/v1/admin")
@RequiredArgsConstructor
@SaCheckRole("admin") // 具备admin角色才可以调用
@SaCheckLogin
public class AdminController {

	private final AdminService adminService;

	/**
	 * 获取仪表盘数据
	 */
	@GetMapping("/dashboard")
	public SaResult getDashboardData() {
		return SaResult.data(adminService.getDashboardData());
	}

	/**
	 * 分页获取用户列表
	 */
	@GetMapping("/users/{pageNum}/{pageSize}")
	public SaResult getUserList(@PathVariable int pageNum, @PathVariable int pageSize) {
		return SaResult.data(adminService.getUserListByPage(pageNum, pageSize));
	}

	/**
	 * 重置用户密码
	 */
	@PostMapping("/users/reset-password/{userId}")
	public SaResult resetUserPassword(@PathVariable String userId) {
		return SaResult.data(adminService.resetUserPassword(userId));
	}

	/**
	 * 删除用户（逻辑删除）
	 */
	@DeleteMapping("/users/{userId}")
	public SaResult deleteUser(@PathVariable String userId) {
		adminService.deleteUser(userId);
		return SaResult.ok("成功删除用户：" + userId);
	}

	/**
	 * 添加用户
	 */
	@PutMapping("/users")
	public SaResult addUser(@RequestBody UserAddReqDTO requestParam) {
		return SaResult.data(adminService.addUser(requestParam));
	}

	/**
	 * 编辑用户
	 */
	@PostMapping("/users")
	public SaResult editUser(@RequestBody UserEditReqDTO requestParam) {
		adminService.editUser(requestParam);
		return SaResult.ok("更新成功");
	}

	/**
	 * 分页获取文件列表
	 */
	@GetMapping("/files/{pageNum}/{pageSize}")
	public SaResult getFileList(
			@PathVariable int pageNum,
			@PathVariable int pageSize,
			@RequestParam(required = false) String fileName,
			@RequestParam(required = false) String fileType,
			@RequestParam(required = false) String status,
			@RequestParam(required = false) String auditStatus,
			@RequestParam(required = false) String sortBy) {
		return SaResult.data(adminService.getFileListByPage(pageNum, pageSize, fileName, fileType, status, auditStatus, sortBy));
	}

	/**
	 * 更新文件状态
	 */
	@PostMapping("/files/status")
	public SaResult updateFileStatus(@RequestBody FileUpdateStatusReqDTO requestParam) {
		adminService.updateFileStatus(requestParam);
		return SaResult.ok("文件状态更新成功");
	}

	/**
	 * 删除文件（逻辑删除）
	 */
	@DeleteMapping("/files/{fileId}")
	public SaResult deleteFile(@PathVariable String fileId) {
		adminService.deleteFile(fileId);
		return SaResult.ok("成功删除文件：" + fileId);
	}
}